package com.example.demo.service.impl;

import com.example.demo.entity.AAccessory;
import com.example.demo.entity.DAccessory;
import com.example.demo.mapper.AAccessoryMapper;
import com.example.demo.service.AAccessoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.example.demo.exception.bizException.BizException;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* <p>
*  成果附件实现类
* </p>
*
* @author cy
* @since 2020-08-01
*/
@Slf4j
@Service
public class AAccessoryServiceImpl extends ServiceImpl<AAccessoryMapper, AAccessory> implements AAccessoryService {

    @Override
    public Page<AAccessory> listAAccessorysByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询aAccessory: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        QueryWrapper<AAccessory> queryWrapper =  new QueryWrapper<AAccessory>().like("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<AAccessory> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询aAccessory完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public AAccessory getAAccessoryById(int id) {
        log.info("正在查询aAccessory中id为{}的数据",id);
        AAccessory aAccessory = super.getById(id);
        log.info("查询id为{}的aAccessory{}",id,(null == aAccessory?"无结果":"成功"));
        return aAccessory;
    }

    @Override
    public List<AAccessory> getAAccessoryByAId(int id) {
        log.info("正在查询aAccessory中aId为{}的数据",id);
        List<AAccessory> list=super.list(new QueryWrapper<AAccessory>().eq("a_id",id));
        log.info("查询成果Aid为{}的附件有{}个",id,list.size());
        return list;
    }

    @Override
    public int insertAAccessory(AAccessory aAccessory) {
        log.info("正在插入aAccessory");
        if (super.save(aAccessory)) {
            log.info("插入aAccessory成功,id为{}",aAccessory.getAccessoryId());
            return aAccessory.getAccessoryId();
        } else {
            log.error("插入aAccessory失败");
            throw new BizException("添加失败");
        }
    }

    @Transactional
    @Override
    public int deleteAAccessoryById(int id) {
        log.info("正在删除id为{}的aAccessory",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的aAccessory成功",id);
            return id;
        } else {
            log.error("删除id为{}的aAccessory失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Transactional
    @Override
    public int updateAAccessory(AAccessory aAccessory) {
        log.info("正在更新id为{}的aAccessory",aAccessory.getAccessoryId());
        if (super.updateById(aAccessory)) {
            log.info("更新d为{}的aAccessory成功",aAccessory.getAccessoryId());
            return aAccessory.getAccessoryId();
        } else {
            log.error("更新id为{}的aAccessory失败",aAccessory.getAccessoryId());
            throw new BizException("更新失败[id=" + aAccessory.getAccessoryId() + "]");
        }
    }

}
